package com.yiren.backstage.judge.thread;

import android.content.Context;

import com.yiren.backstage.config.enums.SiteConfigCodeEnum;
import com.yiren.backstage.currency.bean.CarStateBean;
import com.yiren.backstage.currency.constant.AutoJudgeConstant;
import com.yiren.backstage.currency.constant.SignalGConstant;
import com.yiren.backstage.currency.entity.GradeStandardEntity;
import com.yiren.backstage.currency.entity.ProjectEntity;
import com.yiren.backstage.currency.enums.TaskStateEnum;
import com.yiren.backstage.currency.utils.MessageUtil;
import com.yiren.backstage.db.dao.cache.GradeStandardCache;
import com.yiren.backstage.judge.handler.AutoJudgeHandler;
import com.yiren.backstage.judge.services.jjd.IJjdJudgeThreadService;
import com.yiren.backstage.judge.services.jjd.impl.JjdJudgeThreadServiceImpl;

/**
 * Time:2022/4/21 10:41
 * Author:xiaozan
 * Description:加减挡项目评判线程
 */
public class JjdJudgeThread implements Runnable {

    private AutoJudgeHandler autoJudgeHandler;
    private ProjectEntity projectEntity;
    private IJjdJudgeThreadService iJjdJudgeThreadService;
    private CarStateBean carStateBean;

    /*** 挡位执行顺序 */
    private int gearOperOrder;
    /*** 当前正在执行挡位顺序索引 */
    private int index = 1;
    /*** 是否已播报 */
    private boolean broadcast;
    /*** 每个挡位开始时间 */
    private long startTime;
    /*** 每个挡位开始里程 */
    private float startMileage;
    /*** 项目开始时间 */
    private long projectStartTime;
    /*** 项目开始里程 */
    private float projectStartMileage;

    private int[] gearOperOrderArray;

    public JjdJudgeThread(Context context, AutoJudgeHandler autoJudgeHandler, ProjectEntity projectEntity) {
        this.autoJudgeHandler = autoJudgeHandler;
        this.projectEntity = projectEntity;
        iJjdJudgeThreadService = new JjdJudgeThreadServiceImpl(context, autoJudgeHandler);
        gearOperOrder = iJjdJudgeThreadService.loadGearOperOrder();
        carStateBean = CarStateBean.getInstance();
        transformationArray();
        init();
        projectStartTime = System.currentTimeMillis();
        projectStartMileage = carStateBean.getTotalMileage();
    }

    /**
     * 将挡位操作顺序转换成数组
     */
    private void transformationArray() {
        String order = gearOperOrder + "";
        if (order.equals("0")) return;
        gearOperOrderArray = new int[order.length()];
        for (int i = 0; i < gearOperOrderArray.length; i++) {
            Character character = order.charAt(i);
            gearOperOrderArray[i] = Integer.parseInt(character.toString());
        }
    }

    @Override
    public void run() {
        try {
            while (autoJudgeHandler.getTaskStateEnum() != TaskStateEnum.FORCT_CLOSE) {
                //挡位数组为空 结束项目
                if (null == gearOperOrderArray) {
                    autoJudgeHandler.sendMessage(MessageUtil.newMessage(AutoJudgeConstant.PLAY_PROJECT_END_VOICE, projectEntity));
                    return;
                }

                //项目总超时监控
                if (iJjdJudgeThreadService.jjdProjectIsOverTime(projectStartMileage, projectStartTime)) {
                    autoJudgeHandler.sendMessage(MessageUtil.newMessage(AutoJudgeConstant.ADD_PROJECT_GRADE, jjdGradeStandard()));
                    autoJudgeHandler.sendMessage(MessageUtil.newMessage(AutoJudgeConstant.PLAY_PROJECT_END_VOICE, projectEntity));
                    return;
                }

                //播报提示操作语音
                if (!broadcast) iJjdJudgeThreadService.playOperVoice(gearOperOrderArray, index);
                broadcast = true;

                //检测挡位是否符合操作
                boolean accordGearOper = iJjdJudgeThreadService.accordGearOper(gearOperOrderArray[index]);
                boolean singleGearOperIsOvertime = iJjdJudgeThreadService.singleGearOperIsOvertime(startMileage, startTime);
                //符合挡位操作并且未超时就执行下个挡位
                if (accordGearOper && !singleGearOperIsOvertime) {
                    //播报操作完成的语音
                    iJjdJudgeThreadService.playFinishGearVoice(gearOperOrderArray[index]);

                    //判断加减档操作是否执行完成
                    if (index >= gearOperOrderArray.length - 1) {
                        //结束项目
                        autoJudgeHandler.sendMessage(MessageUtil.newMessage(AutoJudgeConstant.PLAY_PROJECT_END_VOICE, projectEntity));
                        return;
                    } else {
                        //休眠时间播报下个操作
                        int i = 0;
                        int count = ((int) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.J8.toString()) * 1000) / 100;
                        while (autoJudgeHandler.getTaskStateEnum() != TaskStateEnum.FORCT_CLOSE && i <= count) {
                            Thread.sleep(100);
                            i++;
                        }
                    }
                    //执行下个挡位
                    index++;
                    init();
                } else {
                    //单个挡位操作超时
                    if (singleGearOperIsOvertime) {
                        autoJudgeHandler.sendMessage(MessageUtil.newMessage(AutoJudgeConstant.ADD_PROJECT_GRADE, jjdGradeStandard()));
                        autoJudgeHandler.sendMessage(MessageUtil.newMessage(AutoJudgeConstant.PLAY_PROJECT_END_VOICE, projectEntity));
                        return;
                    }
                }

                //判断是否挂错挡 挂错挡结束项目
                boolean wrongGear = iJjdJudgeThreadService.wrongGear(gearOperOrderArray, index);
                if (wrongGear) {
                    //扣分
                    autoJudgeHandler.sendMessage(MessageUtil.newMessage(AutoJudgeConstant.ADD_PROJECT_GRADE, jjdGradeStandard()));
                    //结束项目
                    autoJudgeHandler.sendMessage(MessageUtil.newMessage(AutoJudgeConstant.PLAY_PROJECT_END_VOICE, projectEntity));
                    return;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void init() {
        startMileage = carStateBean.getTotalMileage();
        startTime = System.currentTimeMillis();
        broadcast = false;
    }

    /**
     * 返回加减挡 未按指令平稳加、减挡
     *
     * @return
     */
    private GradeStandardEntity jjdGradeStandard() {
        //指定评分
        GradeStandardEntity gradeStandardEntity = GradeStandardCache.getGradeStandard("60");
        gradeStandardEntity.setProjectEntity(projectEntity);
        return gradeStandardEntity;
    }
}
